package com.xuanyue.db.xuan.sdk.client.pool;

import java.io.IOException;

import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

import com.xuanyue.db.xuan.msg.VLAUETYPE;
import com.xuanyue.db.xuan.msg.X2YMsg;
import com.xuanyue.db.xuan.msg.X2YValue;
import com.xuanyue.db.xuan.sdk.future.SyncWrite;

import io.netty.channel.Channel;
import io.netty.channel.nio.NioEventLoopGroup;
/**
 * @author 解观海
 * @email guanhaixie@sina.cn
 * @date 2020年12月5日
 *
 */
public class XYYChannelGenericObjectPool extends GenericObjectPool<Channel> {

	
	public XYYChannelGenericObjectPool(String host, int port,GenericObjectPoolConfig<Channel> config) {
		super(new ChannelFactory(host, port, new NioEventLoopGroup()));
	}

	@Override
	public void close() {
		try {
			super.close();
		} finally {
			ChannelFactory f=(ChannelFactory)this.getFactory();
			try {
				f.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}

	public static void main(String[] args) {
		GenericObjectPoolConfig<Channel> config = new GenericObjectPoolConfig<>();
		config.setMaxTotal(5);
		config.setMinIdle(3000);
		String host="127.0.0.1";
		int port = 7397;
		XYYChannelGenericObjectPool pool = new XYYChannelGenericObjectPool(host, port, config);
		
		Channel c = null;
		try {
			c = pool.borrowObject();
			X2YMsg request = new X2YMsg();
            request.setMsg( new X2YValue(VLAUETYPE.STRING, " select phone,price,create_time from T_PH where   Phone_seach(phone,Contains,'99') and price>2000f and ismy=true and city>3 order by price  limit 174810,10"));
//            SyncWrite s = new SyncWrite();
            X2YMsg response = SyncWrite.writeAndSync(c, request, 1000);
            response.getData().forEach( e->{
            	System.out.println(e);
            });
            System.out.println(response.getMsg());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			pool.returnObject(c);
		}
		pool.close();
		
		
	}
	
}
